package cn.snowheart.observer.weather.custom.weather;

import cn.snowheart.observer.weather.custom.observer.Observer;
import cn.snowheart.observer.weather.custom.observer.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuwanxiang
 * @version 2019/06/24
 */
public class WeatherData implements Subject {

    private int data;
    private List<Observer> observers;

    public WeatherData() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(data);
        }
    }

    public void measurementsChanged() {
        notifyObservers();
    }

    public void setData(int data) {
        this.data = data;
        measurementsChanged();
    }

}
